package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.vo.TaskInfoVO;
import com.ruoyi.system.domain.vo.TbTaskDetailVO;
import com.ruoyi.system.mapper.TbTaskTargetMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TbTaskDetailMapper;
import com.ruoyi.system.domain.TbTaskDetail;
import com.ruoyi.system.service.ITbTaskDetailService;

/**
 * 任务明细Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-23
 */
@Service
public class TbTaskDetailServiceImpl implements ITbTaskDetailService {
    @Autowired
    private TbTaskDetailMapper tbTaskDetailMapper;


    @Autowired
    private TbTaskTargetMapper tbTaskTargetMapper;

    /**
     * 查询任务明细
     *
     * @param id 任务明细主键
     * @return 任务明细
     */
    @Override
    public TbTaskDetail selectTbTaskDetailById(Long id) {
        return tbTaskDetailMapper.selectTbTaskDetailById(id);
    }

    /**
     * 查询任务明细列表
     *
     * @param tbTaskDetail 任务明细
     * @return 任务明细
     */
    @Override
    public List<TbTaskDetailVO> selectTbTaskDetailList(TbTaskDetail tbTaskDetail) {
        return tbTaskDetailMapper.selectTbTaskDetailList(tbTaskDetail);
    }

    /**
     * 新增任务明细
     *
     * @param tbTaskDetail 任务明细
     * @return 结果
     */
    @Override
    public int insertTbTaskDetail(TbTaskDetail tbTaskDetail) {
        tbTaskDetail.setCreateTime(DateUtils.getNowDate());
        return tbTaskDetailMapper.insertTbTaskDetail(tbTaskDetail);
    }

    /**
     * 修改任务明细
     *
     * @param tbTaskDetail 任务明细
     * @return 结果
     */
    @Override
    public int updateTbTaskDetail(TbTaskDetail tbTaskDetail) {
        tbTaskDetail.setUpdateTime(DateUtils.getNowDate());
        return tbTaskDetailMapper.updateTbTaskDetail(tbTaskDetail);
    }

    /**
     * 批量删除任务明细
     *
     * @param ids 需要删除的任务明细主键
     * @return 结果
     */
    @Override
    public int deleteTbTaskDetailByIds(Long[] ids) {
        return tbTaskDetailMapper.deleteTbTaskDetailByIds(ids);
    }

    /**
     * 删除任务明细信息
     *
     * @param id 任务明细主键
     * @return 结果
     */
    @Override
    public int deleteTbTaskDetailById(Long id) {
        return tbTaskDetailMapper.deleteTbTaskDetailById(id);
    }

    @Override
    public List<TaskInfoVO> selectTbTaskInfoList(Long taskId) {
        List<TaskInfoVO> infoVOS = new ArrayList<>();
        List<TbTaskDetail> tbTaskDetailList = tbTaskDetailMapper.selectTbTaskInfoListByTaskId(taskId);
        for (TbTaskDetail taskDetail : tbTaskDetailList) {
            TaskInfoVO taskInfoVO = new TaskInfoVO();
            taskInfoVO.setTaskDetailId(taskDetail.getTaskId());
            taskInfoVO.setExecutionOrder(taskDetail.getOrderId());
            taskInfoVO.setTaskType(taskDetail.getPushPackageType());
            if (taskDetail.getPushPackageType() == 1) {
                taskInfoVO.setApplicationName(taskDetail.getAppName());
            }
            if (taskDetail.getPushPackageType() == 2) {
                taskInfoVO.setApplicationName(taskDetail.getPackageName());
            }
            taskInfoVO.setPackageVersionnumber(taskDetail.getPackageVersionnumber());
            taskInfoVO.setStartTime(taskDetail.getStartTime());
            taskInfoVO.setEndTime(taskDetail.getEndTime());

            //查询该具体任务执行进度为 100的条数
            int successCount = tbTaskTargetMapper.selectCount(taskDetail.getId());
            //查询总条数
            int totalCount = tbTaskTargetMapper.selectTotalCount(taskDetail.getId());
            // 计算进度百分比（0-100）
            int progressPercentage = 0;
            if (totalCount > 0) {
                progressPercentage = (int) Math.round((successCount * 100.0) / totalCount);
                // 确保百分比在0-100范围内
                progressPercentage = Math.min(100, Math.max(0, progressPercentage));
            }

            // 设置进度百分比
            taskInfoVO.setProgressPercentage(progressPercentage);
            taskInfoVO.setProgressDetail(successCount + "/" + totalCount);
            taskInfoVO.setFailureCount(taskDetail.getFailCount());
            taskInfoVO.setStatus(taskDetail.getStatus());
            taskInfoVO.setStartTime(taskDetail.getStartTime());
            taskInfoVO.setEndTime(taskDetail.getEndTime());

            infoVOS.add(taskInfoVO);
        }
        return infoVOS;
    }
}
